package com.lsw.jgyf.ui.fragment;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lsw.jgyf.R;
import com.lsw.jgyf.adapter.HomeFragment3Adapter;
import com.lsw.jgyf.inject.Constants;
import com.lsw.jgyf.model.StagingCompanyModel;
import com.lsw.jgyf.ui.activity.CompanyDetailsActivity;
import com.lsw.jgyf.ui.base.fragment.BaseFragment;
import com.lsw.pullableview.PullToRefreshLayout;
import com.lsw.pullableview.PullableListView;
import com.lsw.util.InputTools;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by lsw on 2018/7/8.
 */

public class HomeFragment3 extends BaseFragment<HomeFragment3Presenter> implements HomeFragment3Contract.View, PullToRefreshLayout.OnRefreshListener {
    //返回键
    @BindView(R.id.action_bar_left_btn)
    ImageView btnLeft;
    //标题
    @BindView(R.id.action_bar_title)
    TextView tvTitle;
    @BindView(R.id.et_seach)
    EditText et_seach;
    @BindView(R.id.ll_seach)
    LinearLayout ll_seach;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout pullToRefreshLayout;
    @BindView(R.id.pullableListView)
    PullableListView pullableListView;
    HomeFragment3Adapter homeFragment3Adapter;
    private String name_seach = "";
    private int page = Constants.PAGE_START;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_home3;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initEventAndData() {
        tvTitle.setText("公司");
        btnLeft.setVisibility(View.INVISIBLE);
        homeFragment3Adapter = new HomeFragment3Adapter(mContext);
        pullToRefreshLayout.setOnRefreshListener(this);
        pullableListView.setCanPulldown(true);
        pullableListView.setAdapter(homeFragment3Adapter);
        pullableListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, CompanyDetailsActivity.class);
                intent.putExtra("id", homeFragment3Adapter.getList().get(position).getId() + "");
                startActivity(intent);
            }
        });
        et_seach.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //TODO:你自己的业务逻辑
                    name_seach = v.getText().toString().trim();
                    page = Constants.PAGE_START;
                    mPresenter.getData(page, name_seach);
                }
                return false;
            }
        });
        ll_seach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputTools.ShowKeyboard(et_seach);
            }
        });
        mPresenter.getData(page, name_seach);
    }

    @Override
    public void setData(List<StagingCompanyModel> list, boolean isPage) {
        if (page == 1) {
            homeFragment3Adapter.setData(list);
        } else
            homeFragment3Adapter.setData(list);
        pullableListView.setCanPullup(isPage);
    }

    @Override
    public void RefreshFail() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.FAIL);
    }

    @Override
    public void RefreshSucceed() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        page = Constants.PAGE_START;
        mPresenter.getData(page, name_seach);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        mPresenter.getData(++page, name_seach);
    }

    @Override
    protected void lazyLoadData() {

    }
}
